package com.android.li.temperature.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.util.regex.Pattern;

import android.util.Log;

public class ThermalInfoUtil {

    public static int getThermalSize() {
        File[] files = null;
        try {
            // 华为机器，这个目录是只读的所以会读取不到
            File dir = new File("/sys/class/thermal/");

            if (null != dir && dir.canRead()) {
                files = dir.listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File file) {
                        if (Pattern.matches("thermal_zone[0-9]+", file.getName())) {
                            return true;
                        }
                        return false;
                    }
                });
            } else {
                return getThermalSizeGood();
            }
        } catch (Exception e) {
            Log.d(Constant.TAG, "Exception  = " + e.toString());
            return 0;
        }

        if (null == files) {
            return 0;
        }

        return files.length;
    }

    public static int getThermalSizeGood() {
        int count = 0;

        try {
            BufferedReader brValue = null;
            for (int i = 0; i < 100; i++) {
                brValue = new BufferedReader(new FileReader("/sys/class/thermal/thermal_zone" + i + "/temp"));
                if (brValue.readLine() != null) {
                    count = count + 1;
                }
            }

            if (brValue != null) {
                brValue.close();
            }
        } catch (Exception e) {
            Log.d(Constant.TAG, "Exception  = " + e.toString());
        }

        return count;
    }

}
